package com.zbf.tets;

import com.zbf.common.SysInfo;
import com.zbf.ex.NoAuthException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/6/1  21:59
 * 描述:
 */
/*@Aspect
@Component*/
public class TestAop {

    @Pointcut(value = "execution(public * com.zbf.web.*.*(..))")
    public void pointCut(){}


    @Around("pointCut() && @annotation(com.zbf.tets.AuthPromiss)")
    public Object processHabdler(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {

        ServletRequestAttributes requestAttributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        String userInfo = request.getSession().getAttribute(SysInfo.userInfo).toString();

        MethodSignature signature =(MethodSignature)proceedingJoinPoint.getSignature();
        AuthPromiss annotation = signature.getMethod().getAnnotation(AuthPromiss.class);

        if(annotation!=null){
            String value = annotation.value();
            System.out.println(value);
        }else {
            return  proceedingJoinPoint.proceed();
        }

        if(userInfo!=null){
            throw new NoAuthException("暂无权限！");
        }

        return  proceedingJoinPoint.proceed();

    }

}
